/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Apr 27, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits.java2d;

import java.awt.*;

public class Java2DToolkit {
    /**
     *
     * @param g
     * @param s
     * @param x
     * @param y
     * @param f
     * @param c
     */
    public static void paintString(Graphics g,String s,int x, int y,Font f,Color c){
        Font oldf=g.getFont() ;
        Color oldc=g.getColor() ;
        if(f!=null){
           g.setFont(f);

        }
        if(c!=null)
           g.setColor(c);

        g.drawString(s,x,y);
        g.setColor(oldc);
        g.setFont(oldf);
    }
    /**
     * Get the painting size of a string
     * @param s  the string
     * @param f  font on which the string is painted. If null use default, it usually is null when paint a tooltip
     * @return
     */
    public static Dimension getStringPaintingSize(Graphics g,String s,Font f){
        FontMetrics fm=null;
        if(f!=null){
            fm=g.getFontMetrics(f) ;
        }
        else{ //
            fm=g.getFontMetrics();//use default font
        }
        int w= fm.stringWidth(s ) ;
        int h=fm.getHeight() ;
        return new Dimension(w,h);
    }

    public static void paintTooltip(Graphics g,String s,int x, int y,Font f,Color fc,Color bgc) {
            Color oldc=g.getColor() ;
            Font oldf=g.getFont() ;

            Dimension d=getStringPaintingSize(g,s,f);
            int w= d.width+6 ; //6: need little more space than string width
            int h=d.height ;

            if(f!=null){
               g.setFont(f);
            }

            //draw a rect outline around string
            g.setColor(bgc);
            g.fillRect(x, y-h, w-1, h+1);//rectange position is its upper-left corner
            //draw a string
            g.setColor(fc);
            g.drawString(s,x+2,y-2); //string position is its down-left corner

            g.setColor(oldc);
            g.setFont(oldf);

    }
}
